package model;
import java.awt.Color;
import java.util.List;
import java.util.Random;

/**
 * A simple model of a rabbit.
 * Rabbits age, move, breed, and die.
 * 
 * @author Arjan Frans, Alwin Rombout, Jelte Verbree, Brendan Kanters
 * @version 1.0
 */
public class Grass implements Actor
{
    // The maximum number of new grass patches.
    private static final int MAX_LITTER_SIZE = 3;
	private static Color color = new Color(46,184,0);
    private boolean alive;
    private Location location;
    private Field field;
    private static final int MAX_AGE = 10;
    private static final int EXPAND_AGE = 4;
    private int age;
    
    /**
     * create new grass
     * @param field
     * @param location
     */
    public Grass(Field field, Location location)
    {  	
    	this.field = field;
    	setLocation(location);
    	this.alive = true;
    	age = 0;
    }
    
    /**
     * make the grass act
     * @param newGrass
     */
    public void act(List<Actor> newGrass)
    {
        if(isAlive()) { 
        	age++;
            if(age >= MAX_AGE) {
            	this.setDead();      	
            }
            else if(age >= EXPAND_AGE) {
            	grow(newGrass);  
            }
            
        }
    }
    
    /**
     * make the grass grow
     * @param newGrass
     */
    private void grow(List<Actor> newGrass)
    {
        Field field = getField();
        List<Location> free = field.getGrassLocations(getLocation());
        int patches = new Random().nextInt(MAX_LITTER_SIZE);
        for(int b = 0; b < patches && free.size() > 0; b++) {
            Location loc = free.remove(0);
        	Grass grass = new Grass(field, loc);        	            
        	newGrass.add(grass);
        }
    }

    /**
     * get max litter size
     * @return max litter size
     */
	public int getMaxLitterSize() {
		return MAX_LITTER_SIZE;
	}
	
	/**
	 * get color
	 * @return color
	 */
	public Color getColor() {
		return color ;
	}

	@Override
	/**
	 * overrides the isAlive() method
	 * @return boolean
	 */
	public boolean isAlive() {
		return alive;
	}

	@Override
	/**
	 * overrides the setDead() method
	 */
	public void setDead() {
		alive = false;
		field.clearGrass(location);
	}

	@Override
	/**
	 * overrides the getLocation() method
	 * @return location
	 */
	public Location getLocation() {
		return location;
	}

	@Override
	/**
	 * overrides the getField() method
	 * @return field
	 */
	public Field getField() {
		return field;
	}

	@Override
	/**
	 * overrides the setLocation method
	 * @param newLocation
	 */
	 public void setLocation(Location newLocation)
	 {
		if(field.getGrassAt(newLocation) == null) {
			location = newLocation;
	        field.placeGrass(this, newLocation);
		}
        
     }
}

